package com.csthink.common.infrastructure.validation;

import com.csthink.common.infrastructure.validation.annotation.ValidEnumString;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;


/**
 * 枚举字符串注解校验逻辑类
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-09
 */
public class EnumStringValidator implements ConstraintValidator<ValidEnumString, String> {

    private List<String> enumStringList;

    @Override
    public void initialize(ValidEnumString constraintAnnotation) {
        enumStringList = Arrays.asList(constraintAnnotation.value());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return true;
        }

        return enumStringList.contains(value);
    }
}
